// dashboard.js
import { http } from '../../js/http.js';

export const Dashboard = {
    timer: null,
    mount() {
        console.log("【仪表盘】组件挂载");
        this.fetchData();
        this.timer = setInterval(() => {
            this.fetchData();
        }, 5000);
    },
    fetchData() {
        try {
            http.get('/api/dashboard').then(data => {
                // 指标卡片数据
                document.getElementById('active-clients').textContent = data.activeClients;
                document.getElementById('topic-count').textContent = data.topicCount;
                document.getElementById('total-messages').textContent = data.totalMessages;
                document.getElementById('run-time').textContent = data.runTime;

                // 配置信息表格
                document.getElementById('port').textContent = data.port ?? '--';
                document.getElementById('protocol-version').textContent = data.protocolVersion ?? '--';
                document.getElementById('persistence').textContent = data.persistence ?? '--';
                document.getElementById('max-client-queue').textContent = data.maxClientQueue ?? '--';
                document.getElementById('keep-alive').textContent = data.keepAlive ?? '--';
            });

        } catch (e) {
            console.error('获取仪表盘数据失败:', e);
            document.getElementById('active-clients').textContent = '加载失败';
        }
    },
    unmount() {
        if (this.timer) {
            clearInterval(this.timer);
            this.timer = null;
            console.log("【仪表盘】组件已卸载，定时器已清除");
        }
    }
};
